package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.exception.BackUpDataExsitsException;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.model.SystemRecordModel;
import com.cjack.frenchexcel.service.SystemRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/systemRecord")
public class SystemRecordEndpoint extends BaseEndpoint{

    @Autowired
    SystemRecordService service;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list() {

        try{
            //注意pageDate里面的页数是从0开始计算的
            List< SystemRecordModel> datas = service.list();
            for( SystemRecordModel data : datas){
                //处理统计信息展示,把数据库里面保存的key替换成中文
                String backUpCount = data.getBackUpCount();
                backUpCount = backUpCount.replace( CommonConfig.BACKUP_DATA_CONSUMER, CommonConfig.BACKUP_DATA_CONSUMER_MSG);
                backUpCount = backUpCount.replace( CommonConfig.BACKUP_DATA_RECHARGE, CommonConfig.BACKUP_DATA_RECHARGE_MSG);
                backUpCount = backUpCount.replace( CommonConfig.BACKUP_DATA_USER, CommonConfig.BACKUP_DATA_USER_MSG);
                backUpCount = backUpCount.replace( CommonConfig.BACKUP_DATA_GOODS, CommonConfig.BACKUP_DATA_GOODS_MSG);
                data.setBackUpCount( backUpCount);
            }
            return AjaxResult.SUCCESS( datas);
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 备份系统回溯据
     * @return
     */
    @RequestMapping(value = "/backUp", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult backUp( String name) {
        try{
            service.backUp( name);
            return AjaxResult.SUCCESS( "系统数据备份成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 系统初始化
     * @return
     */
    @RequestMapping(value = "/redo", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult redo() {
        try{
            service.redo();
            return AjaxResult.SUCCESS( "系统初始化成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 还原
     * @return
     */
    @RequestMapping(value = "/restore", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult restore( @RequestParam("id") Long id) {
        try{
            service.restore( id);
            return AjaxResult.SUCCESS( "系统数据恢复成功");
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( BackUpDataExsitsException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult del( @RequestParam("ids") String ids) {
        try{
            service.del( ids);
            return AjaxResult.SUCCESS( "删除成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/download", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult download( @RequestParam("id") Long id) {
        try{
            return AjaxResult.SUCCESS( service.download( id));
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
